package com.wyp.didishige.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.wyp.didishige.MyApplication;
import com.wyp.didishige.R;
import com.wyp.didishige.activity.login.LoginActivity;
import com.wyp.didishige.model.User;
import com.wyp.didishige.wiget.dialog.DialogTips;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.listener.UpdateListener;


/**
 * 基类
 *
 * @author smile
 * @ClassName: BaseActivity
 * @Description: TODO
 * @date 2014-6-13 下午5:05:38
 */
@SuppressWarnings("ALL")
public class BaseActivity extends AppCompatActivity {
    protected ActionBar actionBar;
    protected MyApplication mApplication;
    protected String userId;
    protected int mScreenWidth;
    protected int mScreenHeight;
    protected AlertDialog.Builder builder;
    protected ProgressDialog loading;
    protected Toolbar toolbar=null;
    protected FrameLayout mainContent=null;
    private boolean isHideActionBar=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        builder=new AlertDialog.Builder(this);
        loading = new ProgressDialog(this);
        mApplication = MyApplication.getInstance();
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        mScreenWidth = metric.widthPixels;
        mScreenHeight = metric.heightPixels;

        if (Build.VERSION.SDK_INT >22) {  //6.0
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor("#3F51B5"));
        }else{
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        if (isHideActionBar){
            super.setContentView(layoutResID);
        }else{
            View baseContent = getLayoutInflater().inflate(R.layout.activity_base,null);
            toolbar= (Toolbar) baseContent.findViewById(R.id.toolbar);
            mainContent= (FrameLayout) baseContent.findViewById(R.id.main_content);
            View content = getLayoutInflater().inflate(layoutResID,null);
            mainContent.addView(content);
            setSupportActionBar(toolbar);
            super.setContentView(baseContent);
        }

        actionBar=getSupportActionBar();
    }
    /**
     * 更新用户的经纬度信息
     *
     * @param
     * @return void
     * @throws
     * @Title: uploadLocation
     * @Description: TODO
     */
    public void updateUserLocation(BmobGeoPoint point) {
        User u = BmobUser.getCurrentUser(this,User.class);
        if (u != null) {
            final User user = new User();
            user.setLocation(point);
            user.setObjectId(u.getObjectId());
            user.update(this, new UpdateListener() {
                @Override
                public void onSuccess() {
//			    			ShowLog("经纬度更新成功");
                }

                @Override
                public void onFailure(int code, String msg) {
//						ShowLog("经纬度更新 失败:"+msg);
                }
            });
        }

    }
    /**
     * 隐藏ActionBar
     */
    protected void hideActionBar(){
        isHideActionBar=true;
    }
    Toast mToast;

    public void ShowToast(final String text) {
        if (!TextUtils.isEmpty(text)) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (mToast == null) {
                        mToast = Toast.makeText(getApplicationContext(), text,
                                Toast.LENGTH_LONG);
                    } else {
                        mToast.setText(text);
                    }
                    mToast.show();
                }
            });

        }
    }

    public void ShowToast(final int resId) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(BaseActivity.this.getApplicationContext(), resId,
                            Toast.LENGTH_LONG);
                } else {
                    mToast.setText(resId);
                }
                mToast.show();
            }
        });
    }

    /**
     * 打Log
     * ShowLog
     *
     * @return void
     * @throws
     */
    public void ShowLog(String msg) {
        Log.i("life", msg);
    }


    /**
     * 隐藏软键盘
     * hideSoftInputView
     *
     * @param
     * @return void
     * @throws
     * @Title: hideSoftInputView
     */
    public void hideSoftInputView() {
        InputMethodManager manager = ((InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE));
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 显示软键盘
     * hideSoftInputView
     *
     * @param
     * @return void
     * @throws
     * @Title: hideSoftInputView
     */
    public void showSoftInputView(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
    }

    /**
     * 显示下线的对话框
     * showOfflineDialog
     *
     * @return void
     * @throws
     */
    public void showOfflineDialog(final Context context) {
        DialogTips dialog = new DialogTips(this, "您的账号已在其他设备上登录!", "重新登录");
        // 设置成功事件
        dialog.SetOnSuccessListener(new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int userId) {
                MyApplication.getInstance().logout();
                startActivity(new Intent(context, LoginActivity.class));
                finish();
                dialogInterface.dismiss();
            }
        });
        // 显示确认对话框
        dialog.show();
        dialog = null;
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        //锁屏状态下的检测
//        checkLogin();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    protected void init(){
        initParamter();
        initWidget();
        initWidgetListener();
        initWidgetState();
    }

    protected void initParamter(){

    }
    protected void initWidget(){

    }

    protected void initWidgetListener(){

    }

    protected void initWidgetState(){

    }
    /**
     * 动画启动页面 startAnimActivity
     *
     * @throws
     */
    public void startAnimActivity(Intent intent) {
        this.startActivity(intent);
    }
}
